package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.api.ApplicationConstant;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.StrongBoxBean;
import com.panda.pandawash.bean.UserBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.util.WsUtils;
import com.panda.pandawash.view.StatusBarCompat;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.x;

/**
 * Created by gaowf on 2017/2/8.
 */
@ContentView(R.layout.activity_modify_password)
public class ModifyPSDActivity extends Activity {
    public static final String TAG = "API_TAG";
    private EditText mOriginalPsw;
    private EditText mNewPsw;
    private EditText mConfirmPsw;
    private Button mModifyPsw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }

        mOriginalPsw = (EditText)findViewById(R.id.original_password) ;
        mNewPsw = (EditText)findViewById(R.id.new_password) ;
        mConfirmPsw = (EditText)findViewById(R.id.confirm_new_password) ;
        mModifyPsw = (Button)findViewById(R.id.confirm_new_password_button);
    }

    @Event(R.id.btn_modify_psd_return)
    private void excuteReturn(View v) {
        finish();
    }

    @Event(R.id.confirm_new_password_button)
    private void modifyPsw(View v) {
        String password = mOriginalPsw.getText().toString().trim();
        String newPassword = mNewPsw.getText().toString().trim();
        String confirmPassword = mConfirmPsw.getText().toString().trim();


        if (!WsUtils.isMatch(WsUtils.REGEX_PASSWORD,newPassword)) {
            showMessage(getString(R.string.registe_password_legal));
            mNewPsw.requestFocus();
            return;
        }

        if((!newPassword.equals(newPassword.trim()))||(!confirmPassword.equals(confirmPassword.trim()))){
            showMessage(getString(R.string.registe_password_have_space));
            mNewPsw.requestFocus();
            return;
        }
        if(!newPassword.trim().equals(confirmPassword.trim())){
            showMessage(getString(R.string.registe_password_not_same));
            mNewPsw.requestFocus();
            return;
        }
        changeUserPSW();
        //finish();
    }

    private void changeUserPSW(){
        String account = CDApplication.getIntance().getUserInfo().getUseraccount();
        String password = mOriginalPsw.getText().toString();
        String newPassword = mNewPsw.getText().toString();

        HttpUtil.changePSW(new changeUserPSWCallback(),account,account,password,newPassword);
    }

    private class changeUserPSWCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            Log.d(TAG, "SafeBoxSetupCallback statusCode = " + statusCode);
            Log.d(TAG, "SafeBoxSetupCallback onSuccess = " + content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, null);
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if(code.equals("0")){
                        showMessage(getString(R.string.change_psw_success));
                        finish();
                    }else if(code.equals("99")){
                        showMessage(message);
                    }
                }
            } else {
                showMessage(getString(R.string.change_psw_fail));
            }

        }

        @Override
        public void onFailure(Throwable error, String content) {
            super.onFailure(error, content);
            Log.d(TAG, "onFailure = " + error);
            if (error.toString().contains("refused")) {

            }
            showMessage(getString(R.string.change_psw_fail));
        }

    }
    private void showMessage(String string) {
        Toast toast = Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

}
